package com.liy.framework.common.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUtil {
    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);

    public FileUtil() {
    }

    public static File createNewFile(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        return file;
    }

    public static File writerFile(String filePath, InputStream inputStream) throws IOException {
        File file = createNewFile(filePath);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        boolean var5 = true;

        int len;
        while((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }

        fileOutputStream.close();
        inputStream.close();
        return file;
    }

    public static File writerFile(File file, InputStream inputStream) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        byte[] buffer = new byte[1024];
        boolean var4 = true;

        int len;
        while((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }

        fileOutputStream.close();
        inputStream.close();
        return file;
    }

    public static byte[] file2byte(String filePath) {
        byte[] buffer = null;

        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];

            int n;
            while((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }

            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException var7) {
            var7.printStackTrace();
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        return buffer;
    }

    public static byte[] file2byte(File file) {
        byte[] buffer = null;

        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];

            int n;
            while((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }

            fis.close();
            bos.close();
            buffer = bos.toString().getBytes("UTF-8");
        } catch (FileNotFoundException var6) {
            var6.printStackTrace();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

        return buffer;
    }

    public static byte[] file2byteForGZip(File file) throws IOException {
        byte[] b = file2byte(file);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(os);
        gos.write(b);
        gos.finish();
        gos.flush();
        gos.close();
        os.flush();
        os.close();
        System.out.println(os.toString());
        return os.toByteArray();
    }

    public static void byte2File(byte[] buf, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;

        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {
                dir.mkdirs();
            }

            file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(buf);
        } catch (Exception var19) {
            var19.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException var18) {
                    var18.printStackTrace();
                }
            }

            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }

        }

    }

    public static void mkdir(String dir) {
        try {
            File dirPath = new File(dir);
            if (!dirPath.exists()) {
                dirPath.mkdir();
            }
        } catch (Exception var2) {
            log.error("创建目录操作出错: " + var2.getMessage());
            var2.printStackTrace();
        }

    }

    public static void createNewFile(String fileName, String content) {
        try {
            File filePath = new File(fileName);
            if (!filePath.exists()) {
                filePath.createNewFile();
            }

            FileWriter fw = new FileWriter(filePath);
            fw.write(content);
            fw.flush();
            fw.close();
        } catch (Exception var4) {
            log.error("新建文件操作出错: " + var4.getMessage());
            var4.printStackTrace();
        }

    }

    public static void delFile(String fileName) {
        try {
            File delFile = new File(fileName);
            delFile.delete();
        } catch (Exception var2) {
            log.error("删除文件操作出错: " + var2.getMessage());
            var2.printStackTrace();
        }

    }

    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath);
            File myFilePath = new File(folderPath);
            myFilePath.delete();
        } catch (Exception var2) {
            log.error("删除文件夹操作出错" + var2.getMessage());
            var2.printStackTrace();
        }

    }

    public static void delAllFile(String path) {
        File file = new File(path);
        if (file.exists() && file.isDirectory()) {
            String[] childFiles = file.list();
            File temp = null;

            for(int i = 0; i < childFiles.length; ++i) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + childFiles[i]);
                } else {
                    temp = new File(path + File.separator + childFiles[i]);
                }

                if (temp.isFile()) {
                    temp.delete();
                }

                if (temp.isDirectory()) {
                    delAllFile(path + "/" + childFiles[i]);
                    delFolder(path + "/" + childFiles[i]);
                }
            }
        }

    }

    public static void copyFile(String srcFile, String dirDest) {
        try {
            FileInputStream in = new FileInputStream(srcFile);
            mkdir(dirDest);
            FileOutputStream out = new FileOutputStream(dirDest + "/" + (new File(srcFile)).getName());
            byte[] buffer = new byte[1024];

            int len;
            while((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

            out.flush();
            out.close();
            in.close();
        } catch (Exception var6) {
            log.error("复制文件操作出错:" + var6.getMessage());
            var6.printStackTrace();
        }

    }

    public static void copyInputstreamToFile(InputStream is, File file) {
        try {
            FileOutputStream out = new FileOutputStream(file);
            byte[] buffer = new byte[1024];

            int len;
            while((len = is.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

            out.flush();
            out.close();
            is.close();
        } catch (Exception var5) {
            log.error("复制文件操作出错:" + var5.getMessage());
            var5.printStackTrace();
        }

    }

    public static void copyFile(String srcFile, String dirDest, String destFilePath) {
        try {
            FileInputStream in = new FileInputStream(srcFile);
            mkdir(dirDest);
            FileOutputStream out = new FileOutputStream(dirDest + "/" + destFilePath);
            byte[] buffer = new byte[1024];

            int len;
            while((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }

            out.flush();
            out.close();
            in.close();
        } catch (Exception var7) {
            log.error("复制文件操作出错:" + var7.getMessage());
            var7.printStackTrace();
        }

    }

    public static void copyFolder(String oldPath, String newPath) {
        try {
            mkdir(newPath);
            File file = new File(oldPath);
            String[] files = file.list();
            File temp = null;

            for(int i = 0; i < files.length; ++i) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + files[i]);
                } else {
                    temp = new File(oldPath + File.separator + files[i]);
                }

                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    FileOutputStream output = new FileOutputStream(newPath + "/" + temp.getName().toString());
                    byte[] buffer = new byte[2048];

                    int len;
                    while((len = input.read(buffer)) != -1) {
                        output.write(buffer, 0, len);
                    }

                    output.flush();
                    output.close();
                    input.close();
                }

                if (temp.isDirectory()) {
                    copyFolder(oldPath + "/" + files[i], newPath + "/" + files[i]);
                }
            }
        } catch (Exception var10) {
            log.error("复制文件夹操作出错:" + var10.getMessage());
            var10.printStackTrace();
        }

    }

    public static void moveFile(String oldPath, String newPath) {
        copyFile(oldPath, newPath);
        delFile(oldPath);
    }

    public static void moveFiles(String oldPath, String newPath) {
        copyFolder(oldPath, newPath);
        delAllFile(oldPath);
    }

    public static void moveFolder(String oldPath, String newPath) {
        copyFolder(oldPath, newPath);
        delFolder(oldPath);
    }

    public static String zipFiles(String srcDir, String destDir) throws Exception {
        String tempFileName = null;
        byte[] buf = new byte[2048];
        File[] files = (new File(srcDir)).listFiles();
        if (files != null) {
            File[] var6 = files;
            int var7 = files.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                File file = var6[var8];
                if (file.isFile()) {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    if (destDir.endsWith(File.separator)) {
                        tempFileName = destDir + file.getName() + ".zip";
                    } else {
                        tempFileName = destDir + "/" + file.getName() + ".zip";
                    }

                    FileOutputStream fos = new FileOutputStream(tempFileName);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    ZipOutputStream zos = new ZipOutputStream(bos);
                    ZipEntry ze = new ZipEntry(file.getName());
                    zos.putNextEntry(ze);

                    int len;
                    while((len = bis.read(buf)) != -1) {
                        zos.write(buf, 0, len);
                        zos.flush();
                    }

                    bis.close();
                    zos.close();
                }
            }
        }

        return tempFileName;
    }

    public static String readData(InputStream inSream, String charsetName) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        boolean var4 = true;

        int len;
        while((len = inSream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }

        byte[] data = outStream.toByteArray();
        outStream.close();
        inSream.close();
        return new String(data, charsetName);
    }

    public static Set<String> readFile(String path) throws Exception {
        Set<String> datas = new HashSet();
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String line = null;

        while((line = br.readLine()) != null) {
            datas.add(line);
        }

        br.close();
        fr.close();
        return datas;
    }

    public static String getJarFilePath(String sourcePath, Class clazz) throws IOException {
        String systemTempDir = System.getProperty("java.io.tmpdir");
        InputStream stream = clazz.getClassLoader().getResourceAsStream(sourcePath);
        String destination = systemTempDir + File.separator + sourcePath;
        File tmpFile = new File(destination);
        if (tmpFile.exists()) {
            return destination;
        } else {
            if (!tmpFile.getParentFile().exists()) {
                tmpFile.getParentFile().mkdirs();
            }

            byte[] bytes = new byte[1024];
            FileOutputStream downloadFile = new FileOutputStream(destination);

            int index;
            while((index = stream.read(bytes)) != -1) {
                downloadFile.write(bytes, 0, index);
                downloadFile.flush();
            }

            stream.close();
            downloadFile.close();
            return destination;
        }
    }

    public static String getTmpdirFilePath(String filePath) {
        return System.getProperty("java.io.tmpdir") + File.separator + filePath;
    }
}
